<?php
class WeixinPay{
	public $notify_url  = '';//异步处理地址
	public $return_url  = '';//同步处理地址
	public $show_url    = '';//商品展示地址
	public $appid       = '';//appid
	public $sign_type   = 'sha1';//签名方式
	public $mchid       = '';//商户号
	public $key         = '';//通加密串
	public $app_sercert = '';//app sercert
	public $is_debug    = false;//true为调试模式，价格会设置成0.01，false为非调试模式，价格按正常来
	public $logs_path   = '';

	public function __construct() {
		//日志保存目录
		$this->logs_path = dirname(__FILE__) . '/Logs/';
		if(file_exists($this->logs_path) == false){
			mkdir($this->logs_path, true);
		}
    }
	public function init_config(){
		$cert_path = $this->cert_path ? $this->cert_path : dirname(__FILE__) . '/cacert/client_cert.pem';
		$key_path  = $this->key_path ? $this->key_path : dirname(__FILE__) . '/cacert/client_key.pem';
		define('APPID', $this->appid);//微信公众号身份的唯一标识。审核通过后，在微信发送的邮件中查看
		define('MCHID', $this->mchid);//受理商ID，身份标识
		define('KEY', $this->key);//商户支付密钥Key。审核通过后，在微信发送的邮件中查看
		define('APPSECRET', $this->app_sercert);//JSAPI接口中获取openid，审核后在公众平台开启开发模式后可查看

		define('JS_API_CALL_URL','');//获取access_token过程中的跳转uri，通过跳转将code传入jsapi支付页面
		define('SSLCERT_PATH', $cert_path);//证书路径,注意应该填写绝对路径   /cacert/apiclient_cert.pem
		define('SSLKEY_PATH', $key_path);//证书路径,注意应该填写绝对路径    /cacert/apiclient_key.pem
		define('CURL_TIMEOUT', '30');

		include_once dirname(__FILE__) . '/lib/WxPayPubHelper.php';
	}
	public function get_pay_code_url($order_sn, $order_title, $total_fee, $notify_url = ''){
		$this->init_config();
		$unifiedOrder = new UnifiedOrder_pub();
		
		$code_url   = '';
		$notify_url = $notify_url ? $notify_url : $this->notify_url;
		$unifiedOrder->setParameter("body", $order_title);//商品描述
		$unifiedOrder->setParameter("out_trade_no", $order_sn);//商户订单号 
		$unifiedOrder->setParameter("total_fee", $total_fee * 100);//总金额
		$unifiedOrder->setParameter("notify_url", $notify_url);//通知地址 
		$unifiedOrder->setParameter("trade_type", "NATIVE");//交易类型
		
		\Think\Log::write(
			'parameters：' . print_r($unifiedOrder->parameters, true),
			\Think\Log::INFO, 
			'', 
			$this->logs_path . date('Y_m_d') . '.log'
		);
		
		//获取统一支付接口结果
		$unifiedOrderResult = $unifiedOrder->getResult();
		
		// var_dump($unifiedOrderResult);exit;
		//商户根据实际情况设置相应的处理流程
		if ($unifiedOrderResult["return_code"] == "FAIL"){
			//商户自行增加处理流程
			// echo "通信出错：".$unifiedOrderResult['return_msg']."<br>";
		}
		elseif($unifiedOrderResult["result_code"] == "FAIL"){
			//商户自行增加处理流程
			// echo "错误代码：".$unifiedOrderResult['err_code']."<br>";
			// echo "错误代码描述：".$unifiedOrderResult['err_code_des']."<br>";
		}
		elseif($unifiedOrderResult["code_url"] != NULL){
			//从统一支付接口获取到code_url
			$code_url = $unifiedOrderResult["code_url"];
			//商户自行增加处理流程
			//......
		}
		\Think\Log::write(
			'code_url：' . print_r($code_url, true),
			\Think\Log::INFO, 
			'', 
			$this->logs_path . date('Y_m_d') . '.log'
		);
		
    	return $code_url ? $code_url : false;
	}
	//获取微信JS支付调用参数
	public function get_js_pay_call($order_sn, $order_title, $total_fee, $notify_url=''){
		$this->init_config();
		$unifiedOrder = new UnifiedOrder_pub();
		//支付用户的OPENID
		$openid = $this->openid;
		if(!$openid){
			return false;
		}

		$unifiedOrder->setParameter("openid", "$openid");//支付用户的OPENID
		$unifiedOrder->setParameter("body", $order_title);//商品描述
		$unifiedOrder->setParameter("out_trade_no", $order_sn);//商户订单号 
		$unifiedOrder->setParameter("total_fee", $total_fee * 100);//总金额
		$unifiedOrder->setParameter("notify_url", $notify_url ? $notify_url : $this->notify_url);//通知地址 
		$unifiedOrder->setParameter("trade_type", "JSAPI");//交易类型

		$prepay_id = $unifiedOrder->getPrepayId();
		\Think\Log::write('微信支付：prepay_id' . $prepay_id, \Think\Log::INFO, '', $this->logs_path . date('Y_m_d') . '.log');
		//使用jsapi接口
		$jsApi = new JsApi_pub();
		$jsApi->setPrepayId($prepay_id);

		$jsApiParameters = $jsApi->getParameters();
		\Think\Log::write('微信支付：jsApiParameters' . $jsApiParameters, \Think\Log::INFO, '', $this->logs_path . date('Y_m_d') . '.log');

		return $jsApiParameters;
	}
}
?>